package com.school.fuyuan.business.service;

import com.school.fuyuan.business.mapper.RoleMapper;
import com.school.fuyuan.business.model.po.Role;
import com.school.fuyuan.common.util.JsonUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zrhong
 * @version 1.0
 * @date 2019/3/26 21:03
 * @description
 */
@Service
public class RoleService {
    private final RoleMapper roleMapper;

    public RoleService(RoleMapper roleMapper) {
        this.roleMapper = roleMapper;
    }

    public List<Role> getRoles() {
        return roleMapper.getRoles();
    }

    @Transactional(rollbackFor = Exception.class)
    public void addRole(String roleName, List<String> privileges) {
        Role role = new Role();
        role.setRoleName(roleName);
        roleMapper.add(role);
        String roleId = role.getRoleId();
        roleMapper.addRolePrivilege(roleId, privileges);
    }

    @Transactional(rollbackFor = Exception.class)
    public void deleteRole(String roleId) {
        roleMapper.deleteRole(roleId);
        roleMapper.deleteRolePrivilege(roleId);
    }
}
